<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>小马商城</title>
    <script src="modules/js/common.js"></script>
</head>
<body>
<h1 align="center"><img src="image/xiaoma.png" width="50%" height="300"/></h1>

<div class="container">
    <a onclick="doAdmin()">后台管理</a>
    <div id="head" align="right">
        <a href="http://localhost:8080/view/login.html" >登录</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="http://localhost:8080/view/registered.html">注册</a>
    </div>
    <div id="body"></div>
    <div class="pagination"></div>
</div>

<script type="text/javascript">
    window.baseUrl="http://localhost:8080"
    $(function (){
        $(".pagination").load("../modules/common/page.html");
        $("#head").append(``)
        doGetObjects();
        doIfLogin();
    })

    function doIfLogin(){

        if($.cookie("username")!=null){
            let  username=$.cookie("username")
            $("#head").empty();
            $("#head").append(`<span>欢迎${username}</span>
                                <span><a href="manage/itemCar.html">我的购物车</a></span>
                                <span><a href="manage/order.html">我的订单</a></span>
                                <button onclick="doLogout()">退出</button>`)
        }
    }

    function doLogout(){
        $.removeCookie('username', { path: '/' });
        window.location.href="http://localhost:8080/user/logout";
    }
    function doGetObjects(){
        let data={pageCurrent:window.pageCurrent,valid:1,pageSize:12};
        let url="http://localhost:8080/item/";
        console.log(data.pageCurrent+"~~~");
        $.ajax({
            url:url,data:data,
            crossDomain:true,
            xhrFields:{withCredentials:true},
            success(result){
                doInitPageInfo(result);
                console.log(result)
                $("#body").empty();
                result.data.list.forEach((item,i)=>
                    $("#body").append(`<span><a href="/view/item.html"
                        onclick="$.cookie('itemId',${item.id},{path:'/'})">
                     <img src="${item.image}" height="276" width="276"></a></span>
                      `))
                }
        })
    }

    function doAdmin() {
        let url="http://localhost:8080/admin/"
        $.ajax({
            url: url,
            crossDomain: true,
            xhrFields: {withCredentials: true},
            success(result) {
                if(result.state==1){
                    window.location.href="/manage/manage.html"
                }else {
                    alert(result.message);
                }
            }
        })

    }

    </script>
</body>
</html>